package com.minivision.sdktestdemo.ui.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.minivision.sdktestdemo.R;


/**
 * Created by Administrator on 2016/6/28.
 */
public class RectangleColorPicker extends View {
    private Paint mPoint;
    private Point mPos;
    private Bitmap mBackGround;
    private float mRadius;
    private float mScaleX, mScaleY;
    private Rect mSrc, mDes;
    private Paint mBack;

    public RectangleColorPicker(Context context) {
        super(context);
    }

    public RectangleColorPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public RectangleColorPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public void init(Context context, AttributeSet attrs) {
        mBackGround = BitmapFactory.decodeResource(context.getResources(), R.drawable.skin_slide_block_select);
        mPoint = new Paint();
        mPoint.setColor(Color.GRAY);
        mRadius = 45f;
        mBack = new Paint();
        mSrc = new Rect(0, 0, mBackGround.getWidth(), mBackGround.getHeight());
        mDes = new Rect(0, 0, 0, 0);
        mBack.setAntiAlias(true);
        mColor = Color.RED;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mPos == null) {
            mPos = new Point(getWidth() / 2, getHeight() / 2);
        }
        mDes.set(0, 0, getWidth(), getHeight());
        mScaleX = (float)mDes.right / (float)mSrc.right;
        mScaleY = (float)mDes.bottom / (float)mSrc.bottom;
        canvas.drawBitmap(mBackGround, mSrc, mDes, mBack);
        canvas.drawCircle(mPos.x, mPos.y
                , mRadius, mPoint);
    }

    private boolean isOnTheCircle(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        float dis = (x - mPos.x) * (x - mPos.x) + (y - mPos.y) * (y - mPos.y);
        return dis <= mRadius * mRadius;
    }

    private int mColor;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean flag = false;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (isOnTheCircle(event)) {
                    flag = true;
                }
                break;
            case MotionEvent.ACTION_UP:
                flag = true;
                mPos.set((int) event.getX(), (int) event.getY());
                break;
            case MotionEvent.ACTION_MOVE:
                mPos.set((int) event.getX(), (int) event.getY());
                flag = true;
                break;
        }
        invalidate();
        return flag;
    }


    public int getSelectedColor() {
        mColor = mBackGround.getPixel((int)(mPos.x/mScaleX), (int)(mPos.y/mScaleY));
        return mColor;
    }
}
